﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLLMagiPack.Managers;
using ModeloMagiPack.Entidades;

namespace MaGiPack.Movimientos
{
    public partial class FrmCheques : Form
    {
        public Cheque ChequeSeleccionado { get; set; }

        public FrmCheques(bool modoSeleccion)
        {
            InitializeComponent();
            btnSeleccionarCheque.Visible = modoSeleccion;
        }

        private void FrmCheques_Load(object sender, EventArgs e)
        {
            IniciarCalculoSaldo();
            CargarCheques();
        }

        #region Grilla

        private void CargarCheques()
        {
            List<Cheque> lista = new BLLCheque().TraerCheques(txtNumero.Text, chkPropio.Checked);
            dgvCheques.DataSource = (from c in lista
                                     orderby c.FechaVencimiento
                                     select new
                                     {
                                         IdCheque = c.IdCheque,
                                         Numero = c.Numero,
                                         Importe = c.Importe,
                                         FechaVencimiento = c.FechaVencimiento.ToShortDateString(),
                                         CuentaBancaria = new BLLCuentaBancaria().Traer(c.IdCuentaBancaria).Nombre
                                     }).ToList();
            ValidarBotones();
        }

        private void ValidarBotones()
        {
            btnEditar.Enabled = btnRemover.Enabled = btnSeleccionarCheque.Enabled = dgvCheques.SelectedRows.Count > 0;
        }

        private void EditarCheque()
        {
            Cheque cheque = new BLLCheque().TraerCheque((int)dgvCheques.SelectedRows[0].Cells[0].Value);
            FrmCheque frmCheque = new FrmCheque(cheque);
            frmCheque.StartPosition = FormStartPosition.CenterScreen;
            frmCheque.ShowDialog();
            CargarCheques();
        }


        private void dgvCheques_SelectionChanged(object sender, EventArgs e)
        {
            ValidarBotones();
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            FrmCheque frmCheque = new FrmCheque();
            frmCheque.StartPosition = FormStartPosition.CenterScreen;
            frmCheque.ShowDialog();
            CargarCheques();
        }

        private void btnRemover_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Eliminar cheque seleccionado?", "Pregunta", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
            {
                int idCheque = (int) dgvCheques.SelectedRows[0].Cells[0].Value;
                BLLCheque bll = new BLLCheque();
                Cheque cheque = bll.TraerCheque(idCheque);
                bll.EliminarCheque(cheque);
                CargarCheques();
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            EditarCheque();
        }

        private void btnSeleccionarCheque_Click(object sender, EventArgs e)
        {
            ChequeSeleccionado = new BLLCheque().TraerCheque((int)dgvCheques.SelectedRows[0].Cells[0].Value);
            DialogResult = DialogResult.OK;
            Close();
        }

        private void dgvCheques_DoubleClick(object sender, EventArgs e)
        {
            if (dgvCheques.SelectedRows.Count > 0)
                EditarCheque();
        }

        private void btnBuscarCheques_Click(object sender, EventArgs e)
        {
            CargarCheques();
        }

        #endregion

        #region Cálculo Saldo

        private void IniciarCalculoSaldo()
        {
            if (!bgwCalcularSaldo.IsBusy)
            {
                gbSaldo.Text = "Calculando saldo...";
                picCargando.Visible = true;
                bgwCalcularSaldo.RunWorkerAsync();
            }
        }

        private void btnRefrescarSaldo_Click(object sender, EventArgs e)
        {
            IniciarCalculoSaldo();
        }

        private void bgwCalcularSaldo_DoWork(object sender, DoWorkEventArgs e)
        {
            BLLCheque bll = new BLLCheque();
            e.Result = bll.CalcularSaldoActual();
        }

        private void bgwCalcularSaldo_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void bgwCalcularSaldo_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            picCargando.Visible = false;
            gbSaldo.Text = "Saldo actual:";

            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            txtSaldoActual.Text = ((decimal)e.Result).ToString("N2", Utilidades.Cultura);
        }

        #endregion
    }
}
